package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Comments;
import cn.wolfcode.trip.base.query.CommentQuery;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("strategyComment")
public class  CommentController {


    @Autowired
    private ICommentService commentService;
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IRegionService regionService;

    /**
     * 查询攻略评论
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("strategyList")
    public String list(Model model , @ModelAttribute("qo")CommentQuery qo){
        PageInfo result = commentService.query(qo);
        model.addAttribute("result",result);
        System.out.println(result);
        model.addAttribute("strategies",strategyService.selectAll());
        return "comment/strategyList";
    }

    /**
     * 保存评论
     * @param comment
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Comments comment){
        JSONResult result = new JSONResult();
        try {
            commentService.saveOrUpdate(comment);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过目标的id查询评论
     * @param StrategyId
     * @return
     */
    @RequestMapping("getCommentsByStrategyId")
    @ResponseBody
    public JSONResult getCommentsByStrategyId(Long StrategyId){
        JSONResult result = new JSONResult();
        try {
            commentService.getCommentsByStrategyId(StrategyId);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
